import { mock, Random } from 'mockjs';
import fetchMock from 'fetch-mock';
import { baseURL } from './';
import { sleep } from '../tool';

// 加入基础地址
const joinUrl = (url: string) => new RegExp(baseURL + url);
const ju = joinUrl; // 别名

// 创建模拟数据
const toData = (data?: any, length?: number | string) => async () => {
  await sleep(Random.natural(300, 1000)); // 随机暂停时间
  return mock({
    code: 0,
    msg: '模拟数据',
    [length ? `data|${length}` : 'data']: length ? [data] : data,
  });
};
const td = toData; // 别名

/**
 * 注册模拟数据
 */

const tableInfo = {
  id: '@id',
  Batch: '@id',
  Description: '@ctitle(50)',
};

const tableList = {
  'list|20': [tableInfo],
  total: 20,
};

fetchMock.get(ju('/getTableList'), td(tableList)).get(ju('/getTableInfo'), td(tableInfo));
